package com.shsh.safemanage.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class ValueUtil {
	public static Long getLong(Object obj) {
		if (obj == null) {
			return null;
		}

		if (obj instanceof Long) {
			return (Long) obj;
		}
		if (obj instanceof Integer) {
			return ((Integer) obj).longValue();
		}
		if (obj instanceof String) {
			try {
				return Long.parseLong((String) obj);
			} catch (Exception e) {
				return 0L;
			}
		}
		if (obj instanceof BigInteger) {
			return ((BigInteger) obj).longValue();
		}
		if (obj instanceof Blob) {
			try {
				return new Long(obj.toString());
			} catch (Exception e) {
			}
		}
		if (obj instanceof BigDecimal) {
			try {
				return new Long(((BigDecimal) obj).longValue());
			} catch (Exception e) {
			}
		}

		return null;
	}

	public static Integer getInteger(Object obj) {
		if (obj == null) {
			return null;
		}

		if (obj instanceof Integer) {
			return (Integer) obj;
		}
		if (obj instanceof Long) {
			return ((Long) obj).intValue();
		}
		if (obj instanceof String) {
			try {
				return Integer.parseInt((String) obj);
			} catch (Exception e) {
				return 0;
			}
		}
		if (obj instanceof Blob) {
			try {
				return new Integer(obj.toString());
			} catch (Exception e) {
			}
		}

		return null;
	}

	public static String getString(Object obj) {
		if (obj == null) {
			return null;
		}

		return obj.toString();
	}

	public static Boolean getBoolean(Object obj) {
		if (obj == null) {
			return null;
		}

		if (obj instanceof Boolean) {
			return (Boolean) obj;
		} else if (obj instanceof String) {
			String bStr = (String) obj;
			if (bStr.toLowerCase().equals("true")) {
				return true;
			} else {
				return false;
			}
		} else {
			try {
				int num = Integer.parseInt(obj.toString());
				return num > 0;
			} catch (Exception e) {
			}
		}

		return null;
	}

	public static Calendar getCalendar(Object obj) {
		if (obj == null) {
			return null;
		}

		if (obj instanceof Calendar) {
			return (Calendar) obj;
		}

		Calendar calendar = Calendar.getInstance();

		if (obj instanceof Timestamp) {
			Timestamp timestamp = (Timestamp) obj;
			calendar.setTimeInMillis(timestamp.getTime());
		}

		if (obj instanceof Date) {
			Date date = (Date) obj;
			calendar.setTime(date);
		}

		if (obj instanceof java.sql.Date) {
			java.sql.Date date = (java.sql.Date) obj;
			calendar.setTimeInMillis(date.getTime());
		}

		return calendar;
	}
	
	public static Date getDate(Object obj) {
		if (obj == null) {
			return null;
		}
		
		if (obj instanceof Date) {
			return (Date) obj;
		}
		
		if (obj instanceof Calendar) {
			return ((Calendar) obj).getTime();
		}
		
		return null;
	}

	public static String getObjectTrimStringValue(Object obj) {
		if (obj == null) {
			return "";
		}

		return obj.toString().trim();
	}
}
